1
0

beforetest.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. const app=getApp();
  2. var util=require("../../../utils/util")
  3. var utils=require("../../../utils/http");
  4. var stoptime=0;
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. imgUrl:app.globalData.imgUrl,
  11. nextsrc:'',
  12. toolobj:'',
  13. testurl:'',
  14. type:'',
  15. loginFlag:false,
  16. phoneFlag:false,
  17. errorText:'请求错误!',
  18. oneButton: [{text: '确定'}],
  19. share_user_id:'',
  20. canIUseGetUserProfile: false,
  21. shareidflag:false
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. var that=this;
  28. wx.showLoading({
  29. title: '数据加载中...',
  30. })
  31. if (wx.getUserProfile) {
  32. this.setData({
  33. canIUseGetUserProfile: true
  34. })
  35. }
  36. this.setData({
  37. type:options.type,
  38. nextsrc:options.type
  39. })
  40. app.globalData.clientype=options.clientype;
  41. if(!!options.share_user_id){
  42. // 客户点击进来
  43. wx.hideHomeButton();
  44. app.globalData.shareuserid=options.share_user_id;
  45. that.login();
  46. that.setData({
  47. share_user_id:options.share_user_id,
  48. shareidflag:false
  49. })
  50. }else{
  51. that.setData({
  52. shareidflag:false
  53. })
  54. that.getnum();
  55. }
  56. },
  57. login:function(){
  58. var that=this;
  59. wx.login({
  60. success: function (data) {
  61. if (data.errMsg == 'login:ok') {
  62. utils.$post({
  63. url: app.globalData.webUrl + 'api/users/code2session',
  64. data: {
  65. code: data.code,
  66. share: that.data.share_user_id,
  67. client_type:app.globalData.clientype
  68. },
  69. success: function (r) {
  70. if(r.data.code=='0'){
  71. app.globalData.token=r.data.token;
  72. app.globalData.sharepersonobj=r.data.share?r.data.share:r.data.user;
  73. that.getnum();
  74. if(r.data.user!=null&&(!!r.data.user.headimgurl&&r.data.user.headimgurl!=""||!!r.data.user.nickname&&r.data.user.nickname!=""&&r.data.user.nickname!='游客')){
  75. app.globalData.personMsg=r.data.user;
  76. app.globalData.sharename=!!r.data.share?r.data.share.nickname:r.data.user.nickname;
  77. that.setData({
  78. loginFlag:false
  79. })
  80. app.globalData.use_id=r.data.user.id;
  81. app.globalData.userflag=false;//有个人信息
  82. var nickname=r.data.user.nickname;
  83. var phone=r.data.user.phone;
  84. app.globalData.phone=false;//没有手机号
  85. if(r.data.user.phone==''){
  86. wx.hideLoading();
  87. app.globalData.phone=false;//没有手机号
  88. that.setData({
  89. phoneFlag:true
  90. })
  91. }else{
  92. app.globalData.phone=true;//有手机号
  93. that.setData({
  94. phoneFlag:false,
  95. })
  96. }
  97. wx.hideLoading();
  98. }else{
  99. that.setData({
  100. loginFlag:true
  101. })
  102. wx.hideLoading();
  103. that.setData({
  104. errorflag:true,
  105. loginFlag:true
  106. })
  107. }
  108. }
  109. }
  110. })
  111. }
  112. }
  113. })
  114. },
  115. getnum:function(){
  116. let that=this;
  117. utils.$post({
  118. url: app.globalData.webUrl + 'api/tool/toolsnumbers',
  119. header: {
  120. 'Authorization':'bearer '+app.globalData.token
  121. },
  122. data:{
  123. id:that.data.aid,
  124. user:that.data.shareuserid
  125. },
  126. success: function (res) {
  127. if(res.data.code=='0'){
  128. let arr=that.toolobj(res.data.data);
  129. that.setData({
  130. toolobj:arr
  131. })
  132. }
  133. }
  134. })
  135. },
  136. toolobj:function(arr){
  137. var that=this;
  138. var obj={};
  139. for(var i=0;i<arr.length;i++){
  140. obj[arr[i].tool_type]={
  141. share_number:arr[i].share_number,
  142. use_number:arr[i].use_number
  143. }
  144. }
  145. return obj;
  146. },
  147. /**
  148. * 生命周期函数--监听页面初次渲染完成
  149. */
  150. onReady: function () {
  151. },
  152. getUserProfile: function () {
  153. var that=this;
  154. wx.getUserProfile({
  155. lang:'zh_CN',
  156. desc: '为了更好的体验申请获取以下信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  157. success: (res) => {
  158. if(res.errMsg=='getUserProfile:ok'){
  159. utils.$post({
  160. url: app.globalData.webUrl + 'api/users/setUserInfo',
  161. data: {
  162. encryptedData:res.encryptedData,
  163. iv:res.iv,
  164. rawData:res.rawData,
  165. signature:res.signature,
  166. },
  167. header: {
  168. 'Authorization':'bearer '+app.globalData.token
  169. },
  170. success: function (r) {
  171. if(r.data.code=='0'){
  172. that.setData({
  173. loginFlag:false,
  174. phoneFlag:true
  175. })
  176. app.globalData.userflag=false;//有个人信息
  177. that.login();
  178. }else{
  179. that.setData({
  180. errorText:r.data.msg,
  181. errorflag:true,
  182. loginFlag:true
  183. })
  184. }
  185. }
  186. })
  187. }else{
  188. that.setData({
  189. loginFlag:true
  190. })
  191. }
  192. }
  193. })
  194. },
  195. getUserInfoTap:function(res){
  196. var that=this;
  197. if(res.detail.errMsg=='getUserInfo:ok'){
  198. utils.$post({
  199. url: app.globalData.webUrl + 'api/users/setUserInfo',
  200. data: {
  201. encryptedData:res.detail.encryptedData,
  202. iv:res.detail.iv,
  203. rawData:res.detail.rawData,
  204. signature:res.detail.signature,
  205. },
  206. header: {
  207. 'Authorization':'bearer '+app.globalData.token
  208. },
  209. success: function (r) {
  210. if(r.data.code=='0'){
  211. that.setData({
  212. loginFlag:false,
  213. phoneFlag:true
  214. })
  215. app.globalData.userflag=false;//有个人信息
  216. that.login();
  217. }else{
  218. that.setData({
  219. errorText:r.data.msg,
  220. errorflag:true,
  221. loginFlag:true
  222. })
  223. }
  224. }
  225. })
  226. }else{
  227. that.setData({
  228. loginFlag:true
  229. })
  230. }
  231. },
  232. getPhoneNumberTap:function(res){
  233. var that=this;
  234. if(res.detail.errMsg=='getPhoneNumber:ok'){
  235. utils.$post({
  236. url: app.globalData.webUrl + 'api/users/setUserMobile',
  237. data: {
  238. encryptedData:res.detail.encryptedData,
  239. iv:res.detail.iv
  240. },
  241. header: {
  242. 'Authorization':'bearer '+app.globalData.token
  243. },
  244. success: function (r) {
  245. if(r.data.code=='0'){
  246. that.login();
  247. app.globalData.phone=true;
  248. that.setData({
  249. phoneFlag:false
  250. })
  251. }else{
  252. that.setData({
  253. errorText:r.data.msg,
  254. errorflag:true
  255. })
  256. }
  257. }
  258. })
  259. }
  260. },
  261. /**
  262. * 生命周期函数--监听页面显示
  263. */
  264. onShow: function () {
  265. stoptime=0;
  266. setTimeout(function(){
  267. stoptime=stoptime*1+1;
  268. },1000)
  269. },
  270. /**
  271. * 生命周期函数--监听页面隐藏
  272. */
  273. onHide: function () {
  274. },
  275. /**
  276. * 生命周期函数--监听页面卸载
  277. */
  278. onUnload: function () {
  279. },
  280. /**
  281. * 页面相关事件处理函数--监听用户下拉动作
  282. */
  283. onPullDownRefresh: function () {
  284. },
  285. /**
  286. * 页面上拉触底事件的处理函数
  287. */
  288. onReachBottom: function () {
  289. },
  290. nextrouteTap:function(){
  291. wx.navigateTo({
  292. url: "/share/pages/customertool/customertool?type="+this.data.nextsrc,
  293. });
  294. },
  295. addshare:function(){
  296. var that=this;
  297. var type="";
  298. if(that.data.type=='decorateoffer'){
  299. type='priceCalculation';
  300. }else if(that.data.type=='housestyle'){
  301. type='styleTest';
  302. }else{
  303. type='likeTest';
  304. }
  305. utils.$post({
  306. url: app.globalData.webUrl + 'api/tool/tool_share',
  307. header: {
  308. 'Authorization':'bearer '+app.globalData.token
  309. },
  310. data:{
  311. type:type// 装修风格/styleTest 老房翻新/likeTest
  312. },
  313. success: function (res) {
  314. }
  315. })
  316. },
  317. addsharetap: function (type) {
  318. let that = this;
  319. utils.$post({
  320. url: app.globalData.webUrl + 'api/share/addlog',
  321. header: {
  322. 'Authorization': 'bearer ' + app.globalData.token
  323. },
  324. data: {
  325. id: '0',
  326. type: type,
  327. },
  328. success: function (r) {}
  329. })
  330. },
  331. /**
  332. * 用户点击右上角分享
  333. */
  334. onShareAppMessage: function () {
  335. var title;
  336. var that=this;
  337. var img="../../img/sharezqxg.jpg";
  338. that.addsharetap('customerJsAll');
  339. if(this.data.type=='decorateoffer'){
  340. title='装修报价算一算'
  341. img='https://o.nczyzs.com/xcx/pricetool.jpg';
  342. }else if(this.data.type=='housestyle'){
  343. title='装修风格测一测'
  344. img='https://o.nczyzs.com/xcx/styletool.jpg';
  345. }else{
  346. title='老房翻新测一测'
  347. img="../../img/oldbewell.jpg";
  348. }
  349. this.addshare();
  350. return {
  351. title: title, // 默认是小程序的名称(可以写slogan等)
  352. path: '/share/pages/beforetest/beforetest?type=' + this.data.type + "&share_user_id=" + app.globalData.shareuserid+"&clientype="+app.globalData.clientype,
  353. imageUrl: img, //自定义。显示图片长宽比是 5:4
  354.   }
  355. }
  356. })