loginways.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. const { api_wx_login } = require("../../utils/api.js");
  2. const { net } = require("../../utils/net.js");
  3. const util = require("../../utils/util.js");
  4. Page({
  5. data: {
  6. showState: "1",
  7. tel: "400-898-6659",
  8. name: "Hooli Partner", // 小程序名称
  9. buttonWidth: 110, // 按钮宽度,单位px
  10. isSupportButtonApi: true, // agreePrivacyAuthorization api是否在当前版本可用
  11. isAgreeUserAgreement: false, // 是否同意用户协议&隐私政策
  12. },
  13. pageData: {
  14. wxCode: "",
  15. },
  16. onLoad(options) {
  17. // 判断 agreePrivacyAuthorization 是否在当前版本可用
  18. if (wx.canIUse("button.open-type.agreePrivacyAuthorization")) {
  19. // 计算按钮内容区域宽度
  20. this.getButtonContentWidth();
  21. // 展示隐私协议授权按钮
  22. this.setData({
  23. isSupportButtonApi: true,
  24. });
  25. } else {
  26. // 不展示隐私协议授权按钮,向低版本兼容
  27. this.setData({
  28. isSupportButtonApi: false,
  29. });
  30. }
  31. let userInfo = util.getStorageSync("userInfo");
  32. if (userInfo && userInfo.token) {
  33. wx.reLaunch({
  34. url: "/pages/home/home",
  35. });
  36. } else {
  37. this.setData({
  38. showState: "2",
  39. });
  40. wx.login({
  41. success: (res) => {
  42. this.pageData.wxCode = res.code;
  43. },
  44. });
  45. }
  46. },
  47. onReady() {
  48. wx.setNavigationBarTitle({
  49. title: "登录",
  50. });
  51. },
  52. noLogin() {
  53. wx.reLaunch({
  54. url: "/pages/home/home",
  55. });
  56. },
  57. // 用户未同意隐私协议之前点击登录
  58. tapNoAgreeUserAgreement() {
  59. wx.showToast({
  60. title: "请阅读并同意隐私协议",
  61. icon: "none",
  62. duration: 2000,
  63. });
  64. },
  65. // 计算Button按钮内容区域宽度
  66. getButtonContentWidth() {
  67. wx.nextTick(() => {
  68. // wx.createSelectorQuery();
  69. const query = wx.createSelectorQuery().in(this);
  70. const self = this;
  71. query
  72. .select("#buttonContent")
  73. .boundingClientRect((data) => {
  74. // 获取当前选择的节点的宽度
  75. if (data && data.width) {
  76. self.setData({
  77. buttonWidth: data.width + 20,
  78. });
  79. }
  80. })
  81. .exec();
  82. });
  83. },
  84. // 查看用户协议
  85. checkUserAgreement() {
  86. // 打开隐私协议页面
  87. if (wx.openPrivacyContract) {
  88. wx.openPrivacyContract({
  89. success: (e) => {}, // 打开成功
  90. fail: (e) => {
  91. // 打开失败
  92. wx.showToast({
  93. title: "打开失败,请重试",
  94. icon: "none",
  95. duration: 2000,
  96. });
  97. },
  98. });
  99. }
  100. },
  101. // 点击用户协议区域按钮
  102. handleClickUserAgreementButton(e) {
  103. this.setData({
  104. isAgreeUserAgreement: !this.data.isAgreeUserAgreement,
  105. });
  106. },
  107. // 点击用户协议区域
  108. handleClickUserAgreement(e) {
  109. if (!this.data.isSupportButtonApi) {
  110. this.setData({
  111. isAgreeUserAgreement: !this.data.isAgreeUserAgreement,
  112. });
  113. }
  114. },
  115. getPhoneNumber(e) {
  116. let { encryptedData, iv } = e.detail;
  117. if (!encryptedData || !iv) {
  118. return false;
  119. }
  120. let goNext = () => {
  121. wx.login({
  122. success: (res) => {
  123. let { code } = res;
  124. if (!code) {
  125. return false;
  126. }
  127. this.wxLogin(code, encryptedData, iv);
  128. },
  129. });
  130. };
  131. wx.checkSession({
  132. success: () => {
  133. if (this.pageData.wxCode) {
  134. this.wxLogin(this.pageData.wxCode, encryptedData, iv);
  135. } else {
  136. goNext();
  137. }
  138. },
  139. fail: () => {
  140. goNext();
  141. },
  142. });
  143. },
  144. wxLogin(code, encryptedData, iv) {
  145. let params = {};
  146. params.url = api_wx_login;
  147. params.data = {
  148. code,
  149. encryptedData,
  150. iv,
  151. };
  152. net.req(params, true).then(
  153. (res) => {
  154. let { data } = res;
  155. if (data && data.token) {
  156. wx.setStorageSync("userInfo", data);
  157. wx.reLaunch({
  158. url: "/pages/home/home",
  159. });
  160. return false;
  161. }
  162. wx.showModal({
  163. title: "提示",
  164. content: `抱歉,您暂无登录权限,合作请咨询${this.data.tel}`,
  165. showCancel: false,
  166. });
  167. },
  168. (e) => {
  169. wx.showModal({
  170. title: "提示",
  171. content:
  172. (e && e.msg) || `抱歉,您暂无登录权限,合作请咨询${this.data.tel}`,
  173. showCancel: false,
  174. });
  175. }
  176. );
  177. },
  178. handleMobileLogin() {
  179. if (this.data.isAgreeUserAgreement) {
  180. // 同意隐私协议,跳转到手机号登录页面
  181. wx.navigateTo({
  182. url: "/pages/loginmobile/loginmobile",
  183. });
  184. } else {
  185. // 不同意弹出提示
  186. wx.showToast({
  187. title: "请阅读并同意隐私协议",
  188. icon: "none",
  189. duration: 2000,
  190. });
  191. }
  192. },
  193. handleMakePhone() {
  194. let { tel } = this.data;
  195. let systemInfo = util.getStorageSync("systemInfo");
  196. if (!systemInfo) {
  197. systemInfo = wx.getSystemInfoSync();
  198. if (systemInfo) {
  199. wx.setStorageSync("systemInfo", systemInfo);
  200. }
  201. }
  202. let { system } = systemInfo;
  203. if (system.toLowerCase().indexOf("ios") !== -1) {
  204. wx.makePhoneCall({
  205. phoneNumber: tel,
  206. success() {},
  207. fail() {},
  208. complete() {},
  209. });
  210. } else {
  211. wx.showActionSheet({
  212. itemList: [tel],
  213. success(res) {
  214. if (res.tapIndex === 0) {
  215. wx.makePhoneCall({
  216. phoneNumber: tel,
  217. success() {},
  218. fail() {},
  219. complete() {},
  220. });
  221. }
  222. },
  223. });
  224. }
  225. },
  226. });