[Work] must have written new logic inside callbacks success or failure has been encountered several times, this work easy for someone else Zhuadaobabing ...

The new work was 1: Be sure to write logic inside callbacks success or failure has been encountered several times, this work easy for someone else Zhuadaobabing (memory is twice pulling others out, no face.) , thought you were a low-level programmer

Example 1 person

  • Payment method set by the user password, you need twice md5 password encryption, and encryption is asynchronous. Be sure to write the second encryption code for encryption in the first successful callback inside, then the password ajax request after the second encryption saved to the database.

Error writing: Although the program is executed sequentially. But because because the encryption is asynchronous, so the second time encryption and ajax request can be considered simultaneously executed. Based on this situation, the use of ajax access add_paypasswordinterfaces,导致访问该接口传的参数有可能为第一次加密的密码,也有可能为第二次加密的密码。

/**
设置支付密码
 */
function setPayPwd(paySurePassword){

  signature.md5({
      data: paySurePassword
  }, function(ret, err) {
      if (ret.status) {
      console.log("原始一次加密为" + JSON.stringify(ret));
      console.log("一次加密为" + JSON.stringify(ret.value) + "小写为" + ret.value.toLowerCase());

          signature.md5({
              data: "09ES27Ookep2RXA" + ret.value.toLowerCase()
          }, function(ret1, err1) {
              if (ret1.status) {
                    console.log("二次加密为" + JSON.stringify(ret1.value)  + "小写为" +           ret1.value.toLowerCase());
                    secret =  ret1.value.toLowerCase();
                  

      } else {
          alert(JSON.stringify(err));
      }
  });

     /**
      设置支付密码ajax请求,存储到数据库中
     */

      var param = {
                      values: {
                          pay_pass: secret,
                      }
                  };
                  apiAjax("user/add_paypassword", "post", param, function() {}, function(ret) {
                      console.log(JSON.stringify(ret));
                      if(ret.code == 1){// 处理成功的逻辑
                          var user = $api.getStorage('user_info');
                          user.pay_pass = 1234;
                          //重新保存用户信息
                          $api.setStorage('user_info', user);
                      }else{// 处理失败的逻辑

                      }
                      api.toast({
                          msg: ret.msg,
                          duration: 2000,
                          location: 'middle'
                      });
                       api.closeWin();

                  });
            } else {
                alert(JSON.stringify(err1));

            }
        });


      }


}

Correct wording: Second encryption in the first encryption write a successful callback inside. ajax request in writing a second encryption in the success callback.

/**
设置支付密码
 */
function setPayPwd(paySurePassword){
console.log("paySurePassword为" + paySurePassword);
  signature.md5({
      data: paySurePassword
  }, function(ret, err) {
      if (ret.status) {
      console.log("原始一次加密为" + JSON.stringify(ret));
      console.log("一次加密为" + JSON.stringify(ret.value) + "小写为" + ret.value.toLowerCase());

          signature.md5({
              data: "09ES27Ookep2RXA" + ret.value.toLowerCase()
          }, function(ret1, err1) {
              if (ret1.status) {
                  // return ret.value;
                    console.log("二次加密为" + JSON.stringify(ret1.value)  + "小写为" + ret1.value.toLowerCase());
                    secret =  ret1.value.toLowerCase();
                    var param = {
                        values: {
                            pay_pass: secret,
                        }
                    };
                    apiAjax("user/add_paypassword", "post", param, function() {}, function(ret) {
                        console.log(JSON.stringify(ret));
                        if(ret.code == 1){// 处理成功的逻辑
                            // $api.setStorage('payPassword',secret);
                            var user = $api.getStorage('user_info')
                            user.pay_pass = 1234;
                            //重新保存用户信息
                            $api.setStorage('user_info', user);
                        }else{// 处理失败的逻辑

                        }
                        api.toast({
                            msg: ret.msg,
                            duration: 2000,
                            location: 'middle'
                        });
                         api.closeWin();

                    });
              } else {
                  alert(JSON.stringify(err1));

              }
          });

      } else {
          // alert(JSON.stringify(err));
          return JSON.stringify(err);
      }
  });


Example 2 person

  • The current user coordinates and the coordinates of the company to return back, to calculate whether the same city two coordinates corresponding to the display if it is the same city bus route planning, vehicle route planning, route planning and land-line, or show only car route planning.

Error writing: Reverse geocoding company background returned coordinates are not written on the inside reverse geocoding of current location. Since the method is performed asynchronously, so there may be the case: Reverse geocoding current location has not been parsed, and reverse geocoding company returned backstage coordinate the completion of the analysis, the cause of this is the same City latitude and longitude (latLngAddress equal currentLatLngAddress), What, then latLngAddress has not been replicated, will perform latLngAddress not equal currentLatLngAddress logic, so that only car route planning

    var geocoder = new AMap.Geocoder({city: '全国'});

   geocoder.getAddress(currentLngLat, function(status, result) {// 逆向地理编码,获取当前用户坐标 对应的地址信息
     if (status === 'complete' && result.info === 'OK') {
         // result为对应的地理位置详细信息
        latLngAddress = result.regeocode.addressComponent.city;
        console.log("latLngAddress为" + latLngAddress);


     }
   });

   geocoder.getAddress(latLng, function(status, result) {// 逆向地理编码,获取公司后台返回的坐标 对应的地址信息
     if (status === 'complete' && result.info === 'OK') {
         // result为对应的地理位置详细信息
        currentLatLngAddress = result.regeocode.addressComponent.city;
          console.log("currentLatLngAddress" + currentLatLngAddress);
        if(latLngAddress == currentLatLngAddress){// 同一个市
            // 显示公交路线规划、汽车路线规划、陆行路线规划
        }else{
            // 隐藏公交路线规划、陆行路线规划
            $(".busClass").attr("style","display:none");
            $(".walkClass").attr("style","display:none");
        }

     }
   });

Correct wording: Reverse geocoding company returned backstage coordinates written on the inside reverse geocoding current position, make sure that the macro is executed sequentially. Namely: the current position of the reverse geocoding is finished will coordinate the implementation of the company's return to the background of reverse geocoding. Then go latLngAddress determine whether equal currentLatLngAddress is the most accurate approach.

var geocoder = new AMap.Geocoder({city: '全国'});

   geocoder.getAddress(currentLngLat, function(status, result) {// 逆向地理编码,获取当前用户坐标 对应的地址信息
     if (status === 'complete' && result.info === 'OK') {
         // result为对应的地理位置详细信息
        latLngAddress = result.regeocode.addressComponent.city;
        geocoder.getAddress(latLng, function(status, result) {// 逆向地理编码,获取公司后台返回的坐标 对应的地址信息
          if (status === 'complete' && result.info === 'OK') {
              // result为对应的地理位置详细信息
             currentLatLngAddress = result.regeocode.addressComponent.city;

             if(latLngAddress == currentLatLngAddress){// 同一个市
                 // 显示公交路线规划、汽车路线规划、陆行路线规划
             }else{
                 // 隐藏公交路线规划、陆行路线规划
                 $(".busClass").attr("style","display:none");
                 $(".walkClass").attr("style","display:none");
             }

          }
        });

     }
   });

The results: the purpose of the current location from the place of work 1396.3km, show only bus route

2364940-1d6d0e4bd21cab77.png
image.png

2364940-039975a79e766e3e.png
image.png

The new work was 2: After you have finished changing the code, all the test playing box and log commented retain indirect code. Otherwise, you're like a bull in others, careless programmers

Guess you like

Origin blog.csdn.net/weixin_34248258/article/details/90867536