index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. 'use strict';
  2. const {
  3. mpWxGetSessionKey,
  4. mpWxGetPhoneNo
  5. } = require('wx-auth')
  6. const moment = require('moment');
  7. const db = uniCloud.database();
  8. const dbCmd = db.command
  9. const $ = dbCmd.aggregate
  10. const cardCollection = db.collection('card')
  11. const visitorCollection = db.collection('visitor')
  12. const userCollection = db.collection('user');
  13. let uniID = require('uni-id')
  14. const createConfig = require('uni-config-center')
  15. const uniIdConfig = createConfig({
  16. pluginId: 'uni-id'
  17. }).config()
  18. exports.main = async (event, context) => {
  19. uniID = uniID.createInstance({
  20. context
  21. })
  22. console.log('event : ' + JSON.stringify(event))
  23. const {
  24. operation,
  25. data
  26. } = event;
  27. try {
  28. console.log('operation : ' + operation)
  29. switch (operation) {
  30. case "getInfo": {
  31. return true;
  32. }
  33. case "loginByWx": {
  34. return await loginByWx(data);
  35. }
  36. case "getWxConfigPart": {
  37. return await getWxConfigPart(data);
  38. }
  39. case "getPhoneByWeixin": {
  40. return await getPhoneByWeixin(data);
  41. }
  42. case "bindPhone": {
  43. return await bindPhone(data);
  44. }
  45. case "getCard": {
  46. return await getCard(data);
  47. }
  48. case "getMyCard": {
  49. return await getMyCard(data);
  50. }
  51. case "saveCard": {
  52. return await saveCard(data);
  53. }
  54. case "getVisitorList": {
  55. return await getVisitorList(data);
  56. }
  57. case "getMyVisitorList": {
  58. return await getMyVisitorList(data);
  59. }
  60. default: {
  61. throw new Error("未找到接口")
  62. }
  63. }
  64. } catch (e) {
  65. var msg = e.toString().replace("Error: ", "")
  66. console.log("出错了", msg)
  67. return {
  68. code: 50101,
  69. msg: msg
  70. };
  71. }
  72. };
  73. /**
  74. * 绑定手机
  75. */
  76. async function bindPhone() {
  77. var user = await userCollection.where({
  78. _id: data.user_id
  79. }).get();
  80. if (user.data.length == 0)
  81. throw new Error("用户不存在")
  82. await userCollection.doc(data.user_id).update({
  83. phone: data.phone
  84. });
  85. }
  86. /**
  87. * 获取名片
  88. */
  89. async function getCard(data) {
  90. var user = await userCollection.where({
  91. _id: data.uid
  92. }).get();
  93. if (user.data.length == 0)
  94. throw new Error("用户不存在")
  95. var card = await cardCollection.where({
  96. _id: data.id
  97. })
  98. .get();
  99. if (card.data.length == 0) {
  100. return {
  101. code: 200,
  102. data: {
  103. name: "",
  104. phone: "",
  105. job: "",
  106. company: "",
  107. head_img: "",
  108. address: "",
  109. phone: "",
  110. wechat: "",
  111. qq: "",
  112. email: "",
  113. intro: "",
  114. _id: ""
  115. }
  116. }
  117. }
  118. var visitors = await visitorCollection.where({
  119. uid: user.data[0]._id,
  120. card_id: data.id
  121. }).get();
  122. if (visitors.data.length == 0) {
  123. var visitor = {
  124. card_id: data.id,
  125. uid: user.data[0]._id,
  126. name: user.data[0].nickName,
  127. head_img: user.data[0].avatar,
  128. phone: user.data[0].phone
  129. };
  130. var res = await visitorCollection.add(visitor);
  131. }
  132. return {
  133. code: 200,
  134. data: card.data[0]
  135. };
  136. }
  137. /**
  138. * 获取名片
  139. */
  140. async function getMyCard(data) {
  141. var user = await userCollection.where({
  142. _id: data.uid
  143. }).get();
  144. if (user.data.length == 0) {
  145. throw new Error("用户不存在")
  146. }
  147. var card = await cardCollection.where({
  148. uid: data.uid
  149. })
  150. .get();
  151. if (card.data.length == 0) {
  152. return {
  153. code: 200,
  154. data: {
  155. name: "",
  156. phone: "",
  157. job: "",
  158. company: "",
  159. head_img: "",
  160. address: "",
  161. phone: "",
  162. wechat: "",
  163. qq: "",
  164. email: "",
  165. intro: "",
  166. _id: ""
  167. }
  168. }
  169. }
  170. return {
  171. code: 200,
  172. data: card.data[0]
  173. };
  174. }
  175. /**
  176. * 提交名片
  177. */
  178. async function saveCard(data) {
  179. const now = Date.now()
  180. if (data.length == 0) {
  181. throw new Error("请勿提交空内容");
  182. }
  183. var id = "";
  184. var user = await userCollection.where({
  185. _id: data.uid
  186. }).get();
  187. if (user.data.length == 0)
  188. throw new Error("用户不存在")
  189. var card = await cardCollection.where({
  190. uid: user.data[0]._id
  191. })
  192. .get();
  193. if (data.name == "") {
  194. throw new Error("请填写姓名");
  195. }
  196. if (data.phone == "") {
  197. throw new Error("请填写手机号");
  198. }
  199. if (card.data.length == 0) {
  200. data.uid = user.data[0]._id;
  201. data.create_time = now;
  202. var res = await cardCollection.add(data);
  203. console.log("cardCollection.add",res)
  204. id = res.id;
  205. } else {
  206. await cardCollection.doc(card.data[0]._id).update({
  207. name: data.name,
  208. phone: data.phone,
  209. job: data.job,
  210. company: data.company,
  211. head_img: data.head_img,
  212. address: data.address,
  213. phone: data.phone,
  214. wechat: data.wechat,
  215. qq: data.qq,
  216. email: data.email,
  217. intro: data.intro,
  218. create_time: now
  219. });
  220. id = card.data[0]._id;
  221. }
  222. return {
  223. code: 200,
  224. data: {
  225. id: id
  226. }
  227. };
  228. }
  229. /**
  230. * 获取浏览列表
  231. */
  232. async function getVisitorList(data, token, deviceId) {
  233. const now = Date.now()
  234. var total = await visitorCollection.where({
  235. card_id: dbCmd.eq(data.card_id)
  236. }).count();
  237. var res = await visitorCollection.where({
  238. card_id: dbCmd.eq(data.card_id)
  239. })
  240. .orderBy('create_time', 'desc')
  241. .skip(data.skip)
  242. .limit(data.limit)
  243. .get();
  244. return {
  245. code: 200,
  246. data: {
  247. total: total.total,
  248. items: res.data
  249. }
  250. };
  251. }
  252. /**
  253. * 获取浏览列表
  254. */
  255. async function getMyVisitorList(data, token, deviceId) {
  256. const now = Date.now()
  257. var total = await visitorCollection.where({
  258. card_id: dbCmd.eq(data.uid)
  259. }).count();
  260. var res = await visitorCollection.where({
  261. card_id: dbCmd.eq(data.uid)
  262. })
  263. .orderBy('create_time', 'desc')
  264. .skip(data.skip)
  265. .limit(data.limit)
  266. .get();
  267. return {
  268. code: 200,
  269. data: {
  270. total: total.total,
  271. items: res.data
  272. }
  273. };
  274. }
  275. // 通过微信登录
  276. async function loginByWx(data) {
  277. // 获取openid
  278. const wxUser = await mpWxGetSessionKey(data)
  279. console.log("wxUser", wxUser)
  280. if (wxUser.status == 0) {
  281. throw new Error(wxUser.msg)
  282. }
  283. // 通过openid查库里的数据
  284. const result = await userCollection.where({
  285. openId: wxUser.userInfo.openId
  286. }).get()
  287. let id = null;
  288. console.log("result", result.data[0])
  289. const now = moment().format('YYYY-MM-DD HH:mm:ss');
  290. // 如果已经有了,则更新库里的信息
  291. if (result.data.length > 0) {
  292. let user = result.data[0]
  293. const r = await userCollection.doc(user._id).update({
  294. nickName: wxUser.userInfo.nickName,
  295. gender: wxUser.userInfo.gender,
  296. avatar: wxUser.userInfo.avatar,
  297. city: wxUser.userInfo.city,
  298. language: wxUser.userInfo.language,
  299. updateTime: now
  300. })
  301. return {
  302. code: 200,
  303. data: {
  304. id: user._id,
  305. nickName: wxUser.userInfo.nickName,
  306. gender: wxUser.userInfo.gender,
  307. avatar: wxUser.userInfo.avatar,
  308. city: wxUser.userInfo.city,
  309. language: wxUser.userInfo.language,
  310. sessionKey: wxUser.session_key
  311. }
  312. };
  313. } else {
  314. // 若没有,则插入该用户数据
  315. const r = await userCollection.add({
  316. nickName: wxUser.userInfo.nickName,
  317. gender: wxUser.userInfo.gender,
  318. avatar: wxUser.userInfo.avatar,
  319. openId: wxUser.userInfo.openId,
  320. avatar: wxUser.userInfo.avatar,
  321. city: wxUser.userInfo.city,
  322. language: wxUser.userInfo.language,
  323. createTime: now,
  324. updateTime: now
  325. })
  326. return {
  327. code: 200,
  328. data: {
  329. id: r.id,
  330. nickName: wxUser.userInfo.nickName,
  331. openId: wxUser.userInfo.openId,
  332. gender: wxUser.userInfo.gender,
  333. avatar: wxUser.userInfo.avatar,
  334. city: wxUser.userInfo.city,
  335. language: wxUser.userInfo.language,
  336. sessionKey: wxUser.session_key
  337. }
  338. }
  339. }
  340. }
  341. //获取微信授权手机号码
  342. async function getPhoneByWeixin(data) {
  343. var res = await mpWxGetPhoneNo(data);
  344. return {
  345. code: 200,
  346. data: res
  347. }
  348. }
  349. async function getWxConfigPart(data) {
  350. var res = await getWxConfigPart(data);
  351. return {
  352. code: 200,
  353. data: res
  354. }
  355. }