login.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. const util = require('../../utils/util.js');
  2. const md = require('../../utils/md5.js');
  3. const app = getApp();
  4. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  5. Page({
  6. data: {
  7. clickTimes: false,
  8. type: '',
  9. firstLogin: 0,
  10. needRecomCode: 0,
  11. code: '',
  12. show: false,
  13. invitePrice: 0,
  14. flag: true,
  15. shareInfo: {},
  16. vouchers: [{
  17. price: 0,
  18. lowestPrice: 0,
  19. description: '0'
  20. }],
  21. avatarUrl: '',
  22. // 注册
  23. username: "",
  24. phoneNum: "",
  25. password: '',
  26. passwordConfirmation: '',
  27. it: {},
  28. unionId: "",
  29. socialUid: '',
  30. authorization: true,//登录模式 为true是微信登录
  31. formData: {
  32. loginName: '',
  33. password: '',
  34. },
  35. quitLogin: false,//退出登录
  36. registeredShow: false//手机号已注册弹窗
  37. },
  38. // 页面加载 获取小程序码链接参数
  39. onLoad(options) {
  40. this.setData({
  41. quitLogin: options.quitLogin || false
  42. });
  43. let that = this;
  44. if (!util.isEmpty(options.scene)) {
  45. util.ajax({
  46. func: "params",
  47. data: {
  48. "code": options.scene.replace("urlcode%3D", "").replace("urlcode=", "")
  49. },
  50. load: false
  51. }, function (res) {
  52. if (res.code == 0) {
  53. wx.setStorageSync("WX-invite", res.data.code);
  54. that.setData({
  55. type: 'share'
  56. });
  57. if (!util.isObjEmpty(app.globalData.userInfo)) {
  58. wx.reLaunch({
  59. url: '/pages/home/index',
  60. });
  61. }
  62. that.isRecomCode();
  63. }
  64. });
  65. } else {
  66. this.setData({
  67. type: options.type || ''
  68. });
  69. that.isRecomCode();
  70. }
  71. },
  72. // 获取邀请人信息
  73. isRecomCode() {
  74. let that = this,
  75. code = wx.getStorageSync("WX-invite");
  76. if (util.isEmpty(code)) return false;
  77. util.ajax({
  78. func: "inviter/info",
  79. data: {
  80. "code": code
  81. }
  82. }, function (res) {
  83. if (res.code == 0) {
  84. that.setData({
  85. needRecomCode: ((res.data.roleType == 'sharer' || res.data.roleType == 'organ') && res.data.roleTypeDisable == 0 ? 1 : 0),
  86. shareInfo: res.data
  87. });
  88. }
  89. });
  90. },
  91. back() {
  92. this.setData({
  93. show: false
  94. })
  95. // 退出登录
  96. if (this.data.quitLogin) {
  97. wx.reLaunch({
  98. url: '/pages/home/index'
  99. })
  100. } else {
  101. setTimeout(() => {
  102. wx.navigateBack();
  103. }, 200);
  104. }
  105. },
  106. // 切换登录
  107. toggleLogin() {
  108. this.setData({
  109. authorization: !this.data.authorization
  110. })
  111. },
  112. phoneNumInput(e) {
  113. this.setData({
  114. 'formData.loginName': e.detail.value
  115. })
  116. if (this.data.formData.loginName.length == 11) {
  117. this.setData({
  118. passwordFocus: true
  119. })
  120. }
  121. },
  122. passwordInput(e) {
  123. this.setData({
  124. 'formData.password': e.detail.value
  125. })
  126. },
  127. navigatorURl() {
  128. wx.navigateTo({
  129. url: '/pages/account/setting/password?noLogin=true',
  130. });
  131. },
  132. // 手机号密码登录
  133. accountLogin() {
  134. let that = this,
  135. data = that.data.formData
  136. if (util.isPhone(data.loginName)) {
  137. util.showTips("请输入正确的手机号。");
  138. return false;
  139. }
  140. if (util.isEmpty(data.password)) {
  141. util.showTips("请输入密码。");
  142. return false;
  143. }
  144. if (data.password < 6) {
  145. util.showTips("请输入至少6位密码。");
  146. return false;
  147. }
  148. data.password = md.hexMD5(data.password);
  149. util.ajax({
  150. func: "v2/login/account",
  151. data: data
  152. }, function (res) {
  153. if (res.code == 0) {
  154. res.data.role = util.userRole(Math.round(res.data.score));
  155. wx.setStorageSync("WXuserInfo", res.data);
  156. app.globalData.userInfo = res.data
  157. if (that.data.type == "share") {
  158. wx.reLaunch({
  159. url: '/pages/home/index',
  160. });
  161. } else {
  162. // 返回逻辑
  163. that.back();
  164. // wx.navigateBack();
  165. }
  166. } else {
  167. util.showTips(res.reason);
  168. }
  169. that.setData({
  170. clickTimes: false
  171. })
  172. });
  173. },
  174. // 用户登录
  175. getUserProfile() {
  176. let that = this;
  177. this.setData({
  178. clickTimes: true
  179. })
  180. wx.login({
  181. success: res => {
  182. that.setData({
  183. code: res.code
  184. })
  185. }
  186. });
  187. that.userProfile();
  188. },
  189. userProfile() {
  190. let that = this;
  191. wx.getUserProfile({
  192. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中
  193. success: (res) => {
  194. let it = {};
  195. if (!res.encryptedData && !res.iv) {
  196. it.version = "applet_v1.0.0";
  197. it.code = that.data.code;
  198. it.target = 'app';
  199. it.phone = that.data.phoneNum
  200. } else {
  201. it.version = "applet_v1.0.0";
  202. it.code = that.data.code;
  203. it.target = 'app';
  204. it.encryptedData = res.encryptedData;
  205. it.iv = res.iv;
  206. it.phone = that.data.phoneNum
  207. }
  208. that.data.it = it
  209. util.ajax({
  210. func: "applet_login",
  211. data: it
  212. }, function (data) {
  213. if (data.code == 0) {
  214. that.setData({
  215. unionId: data.data.unionId,
  216. socialUid: data.data.socialUid
  217. })
  218. // todo:
  219. // data.data.firstLogin = 1
  220. if (data.data.firstLogin == 1) {
  221. wx.setNavigationBarTitle({ title: '注册' })
  222. that.setData({
  223. flag: false
  224. })
  225. return
  226. }
  227. data.data.role = util.userRole(Math.round(data.data.score));
  228. wx.setStorageSync("WXuserInfo", data.data);
  229. app.globalData.userInfo = data.data
  230. if (that.data.type == "share") {
  231. wx.reLaunch({
  232. url: '/pages/home/index',
  233. });
  234. } else {
  235. that.back();
  236. }
  237. } else {
  238. util.showTips(data.reason);
  239. }
  240. that.setData({
  241. clickTimes: false
  242. })
  243. });
  244. },
  245. fail(rr) {
  246. console.log(67676767, rr)
  247. }
  248. });
  249. },
  250. // 用户注册绑定手机号
  251. getPhoneNumber(e) {
  252. let that = this
  253. let data = {};
  254. if (e.detail.errMsg == "getPhoneNumber:ok") {
  255. data.code = e.detail.code;
  256. util.ajax({
  257. func: "getUserPhone",
  258. data: data,
  259. }, function (data) {
  260. if (data.code == 0) {
  261. that.setData({
  262. phoneNum: data.data
  263. })
  264. // 已注册
  265. } else if (data.code == 1007) {
  266. that.setData({
  267. phoneNum: data.data,
  268. registeredShow: true
  269. })
  270. } else
  271. util.showTips(data.reason);
  272. });
  273. }
  274. },
  275. // 注册
  276. signIn() {
  277. let that = this
  278. if (this.data.username == "" && this.data.avatarUrl == "") {
  279. util.showTips("昵称头像不能为空");
  280. return
  281. } else if (this.data.username == "") {
  282. util.showTips("昵称不能为空");
  283. return
  284. } else if (this.data.avatarUrl == "") {
  285. util.showTips("请点击上传一张属于您自己的头像 ~");
  286. return
  287. } else if (this.data.password == "") {
  288. util.showTips("密码不能为空");
  289. return
  290. } else if (this.data.password.length < 6) {
  291. util.showTips("请输入至少6位密码");
  292. return
  293. } else if (this.data.passwordConfirmation != this.data.password) {
  294. util.showTips("两次密码不一致");
  295. return
  296. }
  297. let data = {};
  298. wx.login({
  299. success: res => {
  300. data.phone = that.data.phoneNum;
  301. data.code = res.code;
  302. data.nickname = that.data.username
  303. data.avatar = that.data.avatarUrl
  304. data.password = md.hexMD5(that.data.password)
  305. data.unionId = that.data.unionId
  306. data.socialUid = that.data.socialUid
  307. data.channelId = util.config.channelInfo.channel //渠道标识
  308. data.city = app.globalData.city.city
  309. // 非列表城市改为空
  310. let cityList = wx.getStorageSync("cityList");
  311. if (data.city && cityList) {
  312. data.city = cityList.indexOf(data.city) == -1 ? '' : data.city
  313. }
  314. util.ajax({
  315. func: "applet/regUser",
  316. data: data,
  317. method: 'POST'
  318. }, function (data) {
  319. if (data.code == 0) {
  320. data.data.role = util.userRole(Math.round(data.data.score));
  321. app.globalData.userInfo = data.data;
  322. // that.data.phoneNum=data.phoneNum
  323. that.setData({
  324. phoneNum: data.data.phoneNum
  325. })
  326. wx.setStorageSync("WXuserInfo", data.data);
  327. if (that.data.needRecomCode == 1) {
  328. wx.navigateTo({
  329. url: '/pages/home/referre?type=' + that.data.type,
  330. });
  331. that.bindCode();
  332. return false
  333. }
  334. // wx.navigateBack()
  335. that.back();
  336. } else
  337. util.showTips(data.reason);
  338. });
  339. }
  340. })
  341. },
  342. onChooseAvatar(e) {
  343. const {
  344. avatarUrl
  345. } = e.detail
  346. let that = this
  347. console.log(that.data.unionId);
  348. // wx.showLoading({ title: '上传准备中' });
  349. // 直传头像
  350. util.uploadFile("oss/policy", avatarUrl, {
  351. unionId: that.data.unionId,
  352. type: "userAvatar"
  353. }, true).then((res) => {
  354. if (res.code == 0) {
  355. that.data.avatarUrl = res.url
  356. this.setData({
  357. avatarUrl: that.data.avatarUrl
  358. })
  359. }
  360. });
  361. },
  362. username(e) {
  363. // console.log(e.detail.value);
  364. this.data.username = e.detail.value
  365. this.setData({
  366. username: this.data.username
  367. })
  368. },
  369. pwSetting(e) {
  370. this.setData({
  371. password: e.detail.value
  372. })
  373. }, pwConfirmation(e) {
  374. this.setData({
  375. passwordConfirmation: e.detail.value
  376. })
  377. },
  378. // 绑定邀请码
  379. bindCode() {
  380. let that = this,
  381. code = wx.getStorageSync("WX-invite");
  382. if (this.data.shareInfo.roleType == 'normal') {
  383. util.showTips("该用户不是分享家/机构,无法绑定邀请码。");
  384. return false;
  385. }
  386. if (this.data.shareInfo.roleTypeDisable == 1) {
  387. util.showTips("该分享家/机构未启用,无法绑定邀请码。");
  388. return false;
  389. }
  390. util.ajax({
  391. func: "user/savecode",
  392. data: {
  393. "code": code,
  394. receiveType: 1
  395. }
  396. }, function (res) {
  397. if (res.code == 0) {
  398. app.globalData.userInfo['recomCode'] = code;
  399. wx.setStorageSync("WXuserInfo", app.globalData.userInfo);
  400. // console.log(code, that.data.shareInfo.roleType)
  401. if (that.data.shareInfo.roleType == 'sharer' || that.data.shareInfo.roleType == 'organ') {
  402. that.setData({
  403. show: true,
  404. vouchers: res.vouchers,
  405. invitePrice: res.invitePrice
  406. });
  407. // console.log(that.data.show)
  408. } else {
  409. that.back();
  410. }
  411. } else
  412. util.showTips(res.reason);
  413. });
  414. },
  415. })