mixin1.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import Vue from "vue";
  2. export default {
  3. data() {
  4. return {
  5. configAll: {
  6. disable_send_province: [],
  7. }, //公共配置
  8. login_show: false,
  9. login_content:
  10. "美美号源将获取您的头像丶昵称丶手机号等信息为您提供优质的服务.",
  11. chanId: "", //站点id
  12. mixin_code: "", //授权code
  13. mixin_type: "", //小程序区别
  14. };
  15. },
  16. onLaunch() {
  17. console.log("onLaunch");
  18. },
  19. onShow() {
  20. this.login_content = "美美号源将登录账号为您提供优质的服务.";
  21. //#ifdef MP-TOUTIAO
  22. this.mixin_type = "TOUTIAO";
  23. this.login_content =
  24. "美美号源将获取您的头像丶昵称等信息为您提供优质的服务.";
  25. tt.showShareMenu({
  26. menus: ["share", "record"],
  27. success(res) {
  28. // console.log("已成功显示转发按钮", res);
  29. },
  30. fail(err) {
  31. // console.log("showShareMenu 调用失败", err.errMsg);
  32. },
  33. complete(res) {
  34. // console.log("showShareMenu 调用完成", res);
  35. },
  36. });
  37. //#endif
  38. //#ifdef H5
  39. this.mixin_type = "H5";
  40. //#endif
  41. // console.log(this.mixin_type)
  42. // 除了H5 小程序需要登录
  43. if (this.mixin_type != "H5") {
  44. if (!uni.getStorageSync("token")) {
  45. this.login_show = true;
  46. this.getcode();
  47. } else {
  48. // 有token 判断是否过期
  49. Vue.prototype.$http.userIndex().then((res) => {
  50. if (res.statusCode == 401) {
  51. uni.removeStorageSync("token");
  52. uni.showToast({
  53. title: "登录失效,请重新进入小程序.",
  54. duration: 2000,
  55. icon: "none",
  56. });
  57. const pages = getCurrentPages();
  58. // 声明一个pages使用getCurrentPages方法
  59. const curPage = pages[pages.length - 1];
  60. // 声明一个当前页面
  61. curPage.onLoad(curPage.options); // 传入参数
  62. curPage.onShow();
  63. curPage.onReady();
  64. }
  65. });
  66. }
  67. //快手分享
  68. if (this.mixin_type != "TOUTIAO") {
  69. ks.showShareMenu({
  70. success(res) {
  71. // console.log("已成功显示转发按钮", res);
  72. },
  73. });
  74. }
  75. }
  76. },
  77. onLoad(option) {
  78. if (option.chanId) {
  79. this.chanId = option.chanId;
  80. uni.setStorageSync("chanId", this.chanId);
  81. }
  82. if (uni.getStorageSync("chanId")) {
  83. this.chanId = uni.getStorageSync("chanId");
  84. }
  85. // 公共配置
  86. this.commonConfig();
  87. },
  88. onHide() {
  89. // console.log('onHide')
  90. // uni.removeStorageSync('chanId');
  91. },
  92. onUnload() {
  93. // console.log('onUnload')
  94. // uni.removeStorageSync('chanId');
  95. },
  96. methods: {
  97. // 用于处理请求腾讯api 跨域问题
  98. jsonp(url, data) {
  99. return new Promise((resolve, reject) => {
  100. // 1.初始化url
  101. let dataString = url.indexOf("?") === -1 ? "?" : "&";
  102. let callbackName = `jsonpCB_${Date.now()}`;
  103. url += `${dataString}callback=${callbackName}`;
  104. if (data) {
  105. // 2.有请求参数,依次添加到url
  106. for (let k in data) {
  107. url += `&${k}=${data[k]}`;
  108. }
  109. }
  110. let scriptNode = document.createElement("script");
  111. scriptNode.src = url;
  112. // 3. callback
  113. window[callbackName] = (result) => {
  114. result ? resolve(result) : reject("没有返回数据");
  115. delete window[callbackName];
  116. document.body.removeChild(scriptNode);
  117. };
  118. // 4. 异常情况
  119. scriptNode.addEventListener(
  120. "error",
  121. () => {
  122. reject("接口返回数据失败");
  123. delete window[callbackName];
  124. document.body.removeChild(scriptNode);
  125. },
  126. false
  127. );
  128. // 5. 开始请求
  129. document.body.appendChild(scriptNode);
  130. });
  131. },
  132. // 快手 授权获取手机号登录
  133. decryptPhoneNumber(e) {
  134. console.log(e.detail);
  135. Vue.prototype.$http
  136. .KuaishouLogin({
  137. code: this.mixin_code,
  138. encryptedData: e.detail.encryptedData,
  139. iv: e.detail.iv,
  140. })
  141. .then((res) => {
  142. if (res.data.code == 1) {
  143. uni.setStorageSync("token", res.data.data.userinfo.token);
  144. this.login_show = false;
  145. const pages = getCurrentPages();
  146. // 声明一个pages使用getCurrentPages方法
  147. const curPage = pages[pages.length - 1];
  148. // 声明一个当前页面
  149. curPage.onLoad(curPage.options); // 传入参数
  150. curPage.onShow();
  151. } else {
  152. uni.showToast({
  153. title: res.data.msg,
  154. duration: 2000,
  155. icon: "none",
  156. });
  157. uni.removeStorageSync("token");
  158. }
  159. });
  160. },
  161. // 取消授权按钮呢
  162. login_cancel() {
  163. console.log("取消");
  164. this.login_show = false;
  165. // 抖音直接退出小程序
  166. //#ifdef MP-TOUTIAO
  167. tt.exitMiniProgram({
  168. success() {
  169. console.log("exited");
  170. },
  171. });
  172. //#endif
  173. },
  174. // 确认按钮
  175. login_confirm() {
  176. // 用户登录
  177. this.userLogin();
  178. },
  179. commonConfig() {
  180. Vue.prototype.$http.commonConfig().then((res) => {
  181. if (res.data.code == 1) {
  182. this.configAll = res.data.data;
  183. console.log(this.configAll);
  184. }
  185. });
  186. },
  187. getcode() {
  188. console.log("getcode");
  189. let this_ = this;
  190. // 获取openid
  191. uni.getProvider({
  192. service: "oauth",
  193. copes: "auth_user",
  194. success: function (res) {
  195. console.log(res.provider[0]);
  196. uni.login({
  197. provider: res.provider[0], //这里服务商名是字节跳动小程序 所以填toutiao
  198. success: function (loginRes) {
  199. console.log(1231, loginRes);
  200. this_.mixin_code = loginRes.code;
  201. },
  202. });
  203. },
  204. });
  205. },
  206. // 抖音登录
  207. userLogin() {
  208. let this_ = this;
  209. // tt.getUserProfile({
  210. // success(res) {
  211. // console.log("getUserProfile 调用成功:", res.userInfo);
  212. // },
  213. // fail(res) {
  214. // console.log("getUserProfile 调用失败", res);
  215. // },
  216. // });
  217. uni.getUserInfo({
  218. withCredentials: true,
  219. success(res) {
  220. console.log(res);
  221. Vue.prototype.$http
  222. .dy_login({
  223. code: this_.mixin_code,
  224. encryptedData: res.encryptedData,
  225. iv: res.iv,
  226. })
  227. .then((res) => {
  228. if (res.data.code == 1) {
  229. this_.login_show = false;
  230. uni.setStorageSync("token", res.data.data.userinfo.token);
  231. const pages = getCurrentPages();
  232. // 声明一个pages使用getCurrentPages方法
  233. const curPage = pages[pages.length - 1];
  234. // 声明一个当前页面
  235. curPage.onLoad(curPage.options); // 传入参数
  236. curPage.onShow();
  237. } else {
  238. this_.login_show = false;
  239. uni.showToast({
  240. title: res.data.msg,
  241. duration: 2000,
  242. icon: "none",
  243. });
  244. uni.removeStorageSync("token");
  245. }
  246. });
  247. },
  248. });
  249. },
  250. // 用于处理请求腾讯api 跨域问题
  251. jsonp(url, data) {
  252. return new Promise((resolve, reject) => {
  253. // 1.初始化url
  254. let dataString = url.indexOf("?") === -1 ? "?" : "&";
  255. let callbackName = `jsonpCB_${Date.now()}`;
  256. url += `${dataString}callback=${callbackName}`;
  257. if (data) {
  258. // 2.有请求参数,依次添加到url
  259. for (let k in data) {
  260. url += `&${k}=${data[k]}`;
  261. }
  262. }
  263. let scriptNode = document.createElement("script");
  264. scriptNode.src = url;
  265. // 3. callback
  266. window[callbackName] = (result) => {
  267. result ? resolve(result) : reject("没有返回数据");
  268. delete window[callbackName];
  269. document.body.removeChild(scriptNode);
  270. };
  271. // 4. 异常情况
  272. scriptNode.addEventListener(
  273. "error",
  274. () => {
  275. reject("接口返回数据失败");
  276. delete window[callbackName];
  277. document.body.removeChild(scriptNode);
  278. },
  279. false
  280. );
  281. // 5. 开始请求
  282. document.body.appendChild(scriptNode);
  283. });
  284. },
  285. },
  286. };