index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. var Parse = getApp().Parse;
  2. var company = getApp().globalData.company
  3. const request = require("../../../utils/request");
  4. const qiniuUploader = require("../../../utils/qiniuUploader");
  5. //获取应用实例
  6. const app = getApp()
  7. /**
  8. * @class NovaAppAuth
  9. * @memberof module:components
  10. * @tutorial userauth
  11. * @desc 通用登录组件
  12. * # 登录组件相关数据表
  13. * - _User
  14. */
  15. Page({
  16. data: {
  17. showModal: false,
  18. logo: "https://file-cloud.fmode.cn/MldI5PBNt7/20210928/g0k1jb034826.png",
  19. name: "未来商城",
  20. desc: "江西脑控科技有限公司是国内领先的IT技术企业。专注于互联网+服务,面向政府提供区块链、大数据、物联网、人工智能解决方案",
  21. wxModel: false,
  22. avatarUrl: '',
  23. avatar: '',
  24. nickname: ''
  25. },
  26. onLoad: async function (options) {
  27. console.log(company)
  28. let Company = new Parse.Query('Company')
  29. Company.equalTo("objectId", company)
  30. let currentCompany = await Company.first()
  31. if (currentCompany && currentCompany.id) {
  32. this.setData({
  33. logo: currentCompany.get('logo'),
  34. name: currentCompany.get('name'),
  35. desc: currentCompany.get('desc')
  36. })
  37. }
  38. this.getUptoken()
  39. },
  40. async getUptoken() {
  41. let res = await Parse.Cloud.run('qiniu_uptoken', {
  42. company: company
  43. })
  44. this.setData({
  45. uptokenURL: res.uptoken,
  46. domain: res.domain,
  47. uploadURL: res.zoneUrl
  48. })
  49. },
  50. onShow: function () {
  51. let userInfo = wx.getStorageSync('userLogin');
  52. if (userInfo != '') {
  53. wx.navigateBack();
  54. };
  55. },
  56. onReady: function () {},
  57. getUserProfile() {
  58. wx.getUserProfile({
  59. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  60. success: async (res) => {
  61. console.log(res);
  62. let myInfo = res.userInfo;
  63. wx.setStorageSync("userLogin", myInfo);
  64. let userInfo = wx.getStorageSync("userInfo");
  65. let appId = app.globalData.appid;
  66. let username, password;
  67. username = userInfo.openid; // openid作为用户名
  68. password = userInfo.openid.substr(username.length - 6, 6); // 后六位作为登录密码
  69. let query = new Parse.Query("_User");
  70. query.equalTo(`wxapp.${appId}.openid`, username);
  71. let current = await query.first();
  72. console.log(current, username, password);
  73. if (current) {
  74. let loginUser = await Parse.User.logIn(username, password);
  75. if (loginUser) {
  76. // 登陆成功则,更新用户资料,并返回用户信息
  77. if (!loginUser.get('avatar') || loginUser.get('nickname') == '微信用户') {
  78. await this.updataUserInfo(myInfo);
  79. }
  80. let u = Parse.User.current()
  81. if (!loginUser.get('mobile')) {
  82. this.showDialogBtn()
  83. return;
  84. } else if (u.get('nickname') == '微信用户' || u.get('nickname') == '' || !u.get('nickname')) {
  85. this.setData({
  86. wxModel: true
  87. })
  88. return
  89. }
  90. setTimeout(() => {
  91. this.backLoad()
  92. }, 500)
  93. }
  94. } else {
  95. let user = new Parse.User();
  96. user.set("username", username);
  97. user.set("password", password);
  98. let wxapp = {};
  99. wxapp[getApp().globalData.appid] = {
  100. openid: userInfo.openid,
  101. uniacid: userInfo.uniacid,
  102. uid: userInfo.id,
  103. };
  104. user.set("wxapp", wxapp);
  105. let signuser = await user.signUp();
  106. if (signuser) {
  107. this.updataUserInfo(myInfo);
  108. this.showDialogBtn()
  109. }
  110. }
  111. this.setData({
  112. userInfo: res.userInfo,
  113. hasUserInfo: true
  114. })
  115. }
  116. })
  117. },
  118. showDialogBtn() {
  119. this.setData({
  120. showModal: true
  121. })
  122. },
  123. async getPhoneNumber(e) {
  124. let {
  125. code
  126. } = e.detail
  127. console.log(code);
  128. let phoneNumber = await request.getPhone(code)
  129. let user = Parse.User.current();
  130. if(phoneNumber) {
  131. user.set("mobile", phoneNumber);
  132. user.save().then(res => {})
  133. }
  134. if (!user.get('avatar') || user.get('nickname') == '微信用户' || !user.get('nickname')) {
  135. this.setData({
  136. showModal: false,
  137. wxModel: true
  138. })
  139. return
  140. }
  141. this.backLoad()
  142. },
  143. hideModal: function () {
  144. this.setData({
  145. showModal: false
  146. })
  147. },
  148. // 用户授权,更新用户信息
  149. async updataUserInfo(myInfo) {
  150. let user = Parse.User.current();
  151. let agentLevel = user.get("agentLevel")
  152. let invite = wx.getStorageSync('invite')
  153. user.set("nickname", myInfo.nickName);
  154. user.set("sex", myInfo.sex);
  155. user.set("city", myInfo.city);
  156. user.set("province", myInfo.province);
  157. user.set("country", myInfo.country);
  158. user.set("avatar", myInfo.avatarUrl);
  159. user.set("company", {
  160. __type: "Pointer",
  161. className: 'Company',
  162. objectId: company
  163. });
  164. if (invite) {
  165. let query = new Parse.Query("_User")
  166. let result = await query.get(invite)
  167. if (result && result.id && result.get("invite") == user.id) {
  168. console.error('邀请人不能是自己的下级')
  169. return
  170. }
  171. // 用户没有邀请人, 并且邀请人不是自己
  172. if (!user.get('invite') && user.id != invite) {
  173. user.set('invite', {
  174. __type: "Pointer",
  175. className: "_User",
  176. objectId: invite
  177. })
  178. user.set('agent', {
  179. __type: "Pointer",
  180. className: "_User",
  181. objectId: invite
  182. })
  183. }
  184. }
  185. if (agentLevel && agentLevel.id) {
  186. user.set("agentLevel", {
  187. __type: "Pointer",
  188. className: 'UserAgentLevel',
  189. objectId: agentLevel.id
  190. });
  191. }
  192. user.save().then(res => {
  193. this.setAccount(res.id)
  194. });
  195. },
  196. async setAccount(user) {
  197. let qeuryAccount = new Parse.Query("Account")
  198. qeuryAccount.equalTo("user", user)
  199. let accountInfo = await qeuryAccount.first()
  200. if (accountInfo && accountInfo.id) {
  201. return
  202. }
  203. let balance = await this.getalance(user)
  204. let Account = Parse.Object.extend("Account")
  205. let account = new Account()
  206. account.set("user", {
  207. __type: 'Pointer',
  208. className: "_User",
  209. objectId: user
  210. })
  211. account.set("company", {
  212. __type: "Pointer",
  213. className: 'Company',
  214. objectId: company
  215. });
  216. account.set("balance", balance)
  217. account.set("bonus", 0)
  218. account.set("credit", 0)
  219. account.save().then(Res => {
  220. return
  221. })
  222. },
  223. async getalance(uid) {
  224. let sql = `select sum("serviceTime") from "ActivityRegister"
  225. where "user" = '${uid}'
  226. and "isComplete" is true
  227. and "company" = 'pmAYN1NesE'
  228. `;
  229. let res = await request.customSQL(sql);
  230. return res[0].sum || 0
  231. },
  232. setInvite() {
  233. let invite = wx.getStorageSync('invite')
  234. if (invite) {
  235. // this.getInvite(Parse.User.current(),invite)
  236. let User = Parse.User.current()
  237. // console.log(User.get('invite'))
  238. // console.log(User.id)
  239. if (invite == User.id || User.get('invite')) {
  240. console.error('已有邀请人或邀请人不能为自己')
  241. return
  242. }
  243. if (!User.get('invite') && invite) {
  244. User.set('invite', {
  245. __type: 'Pointer',
  246. className: '_User',
  247. objectId: invite
  248. })
  249. User.save().then(res => {
  250. console.log(res)
  251. })
  252. }
  253. }
  254. },
  255. backLoad() {
  256. let pages = getCurrentPages();
  257. let beforePage = pages[pages.length - 2];
  258. let options = {
  259. isInit: true
  260. }
  261. beforePage.onLoad(options)
  262. wx.navigateBack({
  263. delta: 1,
  264. })
  265. },
  266. goBack: function () {
  267. wx.navigateBack({
  268. delta: 1,
  269. })
  270. },
  271. //手动获取微信头像
  272. onChooseAvatar(e) {
  273. console.log(e);
  274. let {
  275. avatarUrl
  276. } = e.detail
  277. console.log(avatarUrl);
  278. this.setData({
  279. avatarUrl
  280. })
  281. },
  282. //获取昵称
  283. onChangeName(e) {
  284. console.log('1111');
  285. console.log(e);
  286. },
  287. //确定头像昵称
  288. async onComplete() {
  289. let {
  290. nickname,
  291. avatarUrl
  292. } = this.data
  293. console.log(nickname, avatarUrl);
  294. if (!nickname || !avatarUrl) {
  295. wx.showToast({
  296. title: '请填写完整信息',
  297. icon: 'none',
  298. image: '',
  299. duration: 1500,
  300. mask: false,
  301. });
  302. return
  303. }
  304. //关键注释: https://up-z2.qiniup.com 合法域名必须配置
  305. try {
  306. let avatar = await this.updataAvatar(avatarUrl)
  307. let user = Parse.User.current()
  308. user.set("avatar", avatar)
  309. user.set("nickname", nickname)
  310. user.save().then(res => {
  311. this.onClose()
  312. this.backLoad()
  313. })
  314. } catch (err) {
  315. console.warn('https://up-z2.qiniup.com 合法域名必须配置');
  316. console.log(err);
  317. this.onClose()
  318. this.backLoad()
  319. }
  320. },
  321. //关闭弹窗
  322. onClose() {
  323. this.setData({
  324. wxModel: false
  325. })
  326. },
  327. //上传头像
  328. updataAvatar(url) {
  329. let that = this;
  330. return new Promise((resolve, rejcet) => {
  331. qiniuUploader.upload(
  332. url,
  333. async (res) => {
  334. let img = res.imageURL;
  335. resolve(img)
  336. },
  337. (error) => {
  338. console.log("error: " + error);
  339. resolve(false)
  340. }, {
  341. region: "SCN",
  342. uploadURL: that.data.uploadURL,
  343. domain: that.data.domain,
  344. uptoken: that.data.uptokenURL,
  345. }
  346. );
  347. })
  348. },
  349. })