index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //进入openid处理流程
  2. $.authAjax({
  3. url: api+"marketing/weixin/openidProcess"
  4. // success: function () {
  5. // $.showLoading('数据加载中');
  6. // }
  7. });
  8. //获取活动标题
  9. $.authAjax({
  10. url: api+"marketing/weixin/getActivityTitle",
  11. success: function (res) {
  12. document.title = res;
  13. }
  14. });
  15. //页面结构加载完成后执行
  16. $(function() {
  17. //判断活动是否过期
  18. if (!activityIsExpired()) {
  19. return false;
  20. }
  21. //获取并显示码对应的奖励,如果return false,就不继续向下执行
  22. if (!showReward()) {
  23. return false;
  24. }
  25. //绑定单击事件
  26. $("#receiveReward").on("click",function () {
  27. var img = $('#receiveReward').children("img");
  28. if (img.attr("src") == "images/gray_btn.png"){
  29. receiveReward();
  30. }
  31. });
  32. $(".b-close").on("click",function () {
  33. $.closePopup();
  34. });
  35. $("#explain_popup").on("click",function () {
  36. $('#explain').popup();
  37. });
  38. $("#register_button").on("click",function () {
  39. register();
  40. });
  41. //获取js-sdk签名
  42. createJsapiSignature(currentUrl);
  43. //config信息验证成功后会执行ready方法
  44. wx.ready(function() {
  45. getLocation(
  46. function (latitude,longitude) {
  47. scanRecord(latitude,longitude);
  48. localStorage.setItem('agreeLocation','true');
  49. },
  50. function () {
  51. scanRecord('0.0','0.0');
  52. localStorage.setItem('agreeLocation','false');
  53. }
  54. );
  55. });
  56. //config信息验证失败后会执行error方法
  57. wx.error(function(res){
  58. console.log(res);
  59. $.toast("获取凭据失败", "cancel");
  60. });
  61. $("#birthDate").datetimePicker({
  62. times: function () {
  63. return [];
  64. }
  65. });
  66. });
  67. //判断活动是否过期
  68. function activityIsExpired(){
  69. var flag = true;
  70. $.authAjax({
  71. url: api+"marketing/weixin/activityIsExpired",
  72. async: false,
  73. success: function (res) {
  74. //活动未开始或已结束
  75. if (res.code != 0) {
  76. $('#prompt').text(res.msg);
  77. // $.hideLoading();
  78. flag = false;
  79. }
  80. }
  81. })
  82. return flag;
  83. }
  84. //获取并显示码对应的奖励
  85. function showReward() {
  86. var flag = true;
  87. $.authAjax({
  88. url: api+"marketing/weixin/calculateReward",
  89. async: false,
  90. success: function (res) {
  91. $('#prompt').text(res.msg);
  92. $("#reward").text(res.data);
  93. //二维码失效
  94. if (res.code != 0) {
  95. $('#rewardBtn').attr("src","images/gray_btn.png");
  96. flag = false;
  97. } else {
  98. $('#unit').show();
  99. if (res.data == 0) {
  100. $('#rewardBtn').attr("src","images/gray_btn.png");
  101. } else {
  102. $('#rewardBtn').attr("src","images/btn.png");
  103. }
  104. }
  105. // $.hideLoading();
  106. }
  107. })
  108. return flag;
  109. }
  110. //记录活动(扫码)事件
  111. function scanRecord(latitude,longitude) {
  112. // alert("记录扫码事件");
  113. $.authAjax({
  114. url: api+"marketing/weixin/scanRecord",
  115. type: "POST",
  116. data: {
  117. latitude: latitude,
  118. longitude:longitude,
  119. }
  120. })
  121. }
  122. //领取奖励,如果return false,就不继续向下执行
  123. function receiveReward() {
  124. if (!userIsSubscribeApp()) {
  125. $("#mpQrcode").popup();
  126. return false;
  127. }
  128. if (!userIsAgreeLocation()) {
  129. $.alert("未能获取到位置信息,无法领取!请重新扫码参与活动");
  130. return false;
  131. }
  132. if (!userIsRegister()) {
  133. $("#register").popup();
  134. return false;
  135. }
  136. awardProcess();
  137. }
  138. //用户是否关注公众号
  139. function userIsSubscribeApp() {
  140. var userIsSubscribeApp;
  141. $.authAjax({
  142. url : api+"marketing/weixin/userIsSubscribeApp",
  143. async: false,
  144. success: function (res) {
  145. userIsSubscribeApp = res.data;
  146. if (!userIsSubscribeApp) {
  147. //读取公众号二维码
  148. $('#mpQrcodeImg').attr("src",res.msg);
  149. }
  150. }
  151. })
  152. return userIsSubscribeApp;
  153. }
  154. //用户是否同意地理位置授权
  155. function userIsAgreeLocation() {
  156. var agreeLocation = localStorage.getItem('agreeLocation');
  157. return agreeLocation == "true";
  158. }
  159. //用户是否注册(即含地理位置信息)
  160. function userIsRegister() {
  161. var userIsRegister;
  162. $.authAjax({
  163. url : api+"marketing/weixin/userIsRegister",
  164. async: false,
  165. success: function (data) {
  166. userIsRegister = data;
  167. }
  168. })
  169. if (!userIsRegister) {
  170. //填入昵称和性别数据
  171. $.authAjax({
  172. url: api+"marketing/weixin/getWxUserInfo",
  173. success: function (res) {
  174. $('input[name="nickName"]').val(res.nickname);
  175. $('input[name="sex"][value="'+res.sex+'"]').prop('checked',true);
  176. }
  177. });
  178. }
  179. return userIsRegister;
  180. }
  181. //奖励处理流程
  182. function awardProcess() {
  183. $.showLoading('奖励发放中');
  184. getLocation(
  185. function (latitude,longitude) {
  186. $.authAjax({
  187. url : api+"marketing/weixin/awardProcess",
  188. async: false,
  189. data: {
  190. latitude: latitude,
  191. longitude: longitude
  192. },
  193. success: function (res) {
  194. $.hideLoading();
  195. if (res.code == 0) {
  196. $.toast(res.msg);
  197. $('#rewardBtn').attr("src","images/gray_btn.png");
  198. $("#receiveReward").unbind("click");
  199. } else {
  200. $.toast(res.msg, "cancel");
  201. }
  202. },
  203. error: function (res) {
  204. $.hideLoading();
  205. $.toast("请求失败", "cancel");
  206. }
  207. })
  208. }
  209. );
  210. }
  211. //注册
  212. function register() {
  213. var phone = $('input[name="phone"]').val();
  214. var birthDate = $('input[name="birthDate"]').val();
  215. var nickName = $('input[name="nickName"]').val();
  216. var sex = $('input[name="sex"]').val();
  217. if (phone == "") {
  218. $.toptip("手机号不能为空");
  219. return false;
  220. } else {
  221. var TEL_REGEXP = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/;
  222. if (!TEL_REGEXP.test(phone)) {
  223. $.toptip("手机号格式不正确");
  224. return false;
  225. }
  226. }
  227. if (birthDate == "") {
  228. $.toptip("生日不能为空");
  229. return false;
  230. }
  231. if (nickName == "") {
  232. $.toptip("昵称不能为空");
  233. return false;
  234. }
  235. if (sex == "") {
  236. $.toptip("性别不能为空");
  237. return false;
  238. }
  239. $.showLoading('数据提交中');
  240. getLocation(
  241. function (latitude,longitude) {
  242. var arr = $("#registerForm").serializeArray();
  243. arr.push({name:"latitude",value:latitude},{name:"longitude",value:longitude});
  244. var json = {};
  245. $.each(arr, function () {
  246. json[this.name] = this.value;
  247. });
  248. $.authAjax({
  249. url: api+"marketing/weixin/terminalUser",
  250. type: "PUT",
  251. data: {
  252. json:JSON.stringify(json)
  253. },
  254. success: function (data) {
  255. $.hideLoading();
  256. if (data.code == 0) {
  257. $.toast(data.msg);
  258. $.closePopup();
  259. }
  260. },
  261. error: function (data) {
  262. $.hideLoading();
  263. $.toast("请求失败", "cancel");
  264. }
  265. });
  266. }
  267. );
  268. }