let config = require("../config/config.js");
var app = getApp();
let host = "https://btest.malls.iformall.com/B"
const func = {
  getMallIcon: function () {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.getMallIcon,
        data: {
          appId: config.weapp.appId,
        },
        success: function (res) {
          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          //console.log("getMallIcon complete:" + res)
        }
      })
    })
  },
  userLogin: function (phone, password, latitude, longitude) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.login,
        data: {
          appId: config.weapp.appId,
          phone: phone,
          password: password,
          latitude: latitude,
          longitude: longitude
        },
        method: "POST",
        success: function (res) {
          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          //console.log("userLogin complete:" + res)
        }
      })
    })
  },

  getUserInfo: function (phone, password, latitude, longitude) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.userDetail,
        header: {
          "content-type": "application/json;charset=UTF-8",
          "token": app.globalData.token
        },
        data: {
          appId: config.weapp.appId,
          phone: phone,
          password: password,
          latitude: latitude,
          longitude: longitude
        },
        method: "POST",
        success: function (res) {
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          //console.log("userLogin complete:" + res)
        }
      })
    })
  },

  /**
   * 优惠券查询
   */
  getCouponList: function (pageNum, pageSize) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.couponList,
        header: {
          "content-type": "application/json;charset=UTF-8",
          "token": app.globalData.token
        },
        method: "GET",
        data: {
          pageNum: pageNum,
          pageSize: pageSize
        },
        success: function (res) {
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          console.log(res)
        }
      })
    })
  },
  /**
   * 优惠券详情
   */
  getCouponOrderDetail: function (couponOrderId) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.couponOrderDetail,
        header: {
          "content-type": "application/json;charset=UTF-8",
          "token": app.globalData.token
        },
        method: "GET",
        data: {
          couponOrderId: couponOrderId
        },
        success: function (res) {
          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          console.log(res)
        }
      })
    })
  },
  /**
   * 获取图文详情html
   */
  getHtml: function (couponOrderId) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.couponHtmlDetail,
        header: {
          "content-type": "application/json;charset=UTF-8",
          "token": app.globalData.token
        },
        method: "GET",
        data: {
          couponOrderId: couponOrderId
        },
        success: function (res) {
          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          console.log(res)
        }
      })
    })
  },
  /**
   * 更新密码
   */
  updateUserPassword: function (phone, yzm, newPwd) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.userUpdatePwd,
        header: {
          "content-type": "application/json;charset=UTF-8",
        },
        method: "POST",
        data: {
          phone: phone,
          code: yzm,
          pwd: newPwd,
          appId: config.weapp.appId
        },
        success: function (res) {
          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          console.log(res)
        }
      })
    })
  },
  /**
   * 验证码验证
   */
  hasValidationCode: function (phone, yzm) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.hasValidationCode,
        header: {
          "content-type": "application/json;charset=UTF-8",
        },
        method: "GET",
        data: {
          phone: phone,
          code: yzm,
          type: 1,
          appid: config.weapp.appId
        },
        success: function (res) {
          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          console.log(res)
        }
      })
    })
  },
  /**
   * 发送验证码
   */
  sendValidationCode: function (phone) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.sendValidationCode,
        header: {
          "content-type": "application/json;charset=UTF-8",
        },
        method: "GET",
        data: {
          phone: phone,
          type: 1,
          appid: config.weapp.appId
        },
        success: function (res) {
          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          console.log(res)
        }
      })
    })
  },
  /**
   * 券核销
   */
  couponOrderVerify: function (couponOrderId, verifyRemark) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.couponOrderVerify,
        header: {
          "content-type": "application/json;charset=UTF-8",
          "token": app.globalData.token,
        },
        data: {
          couponOrderId: couponOrderId,
          verifyRemark: verifyRemark,
        },
        method: "POST",
        success: function (res) {
          resolve(res.data)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          //console.log("userLogin complete:" + res)
        }
      })
    })
  },
  /**
   * 
   * @param {*换取openId} code 
   */
  getOpenId: function (code,bUserId) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.getOpenId,
        header: {
          "content-type": "application/json;charset=UTF-8",
          "token": app.globalData.token,
        },
        data: {
          appId: config.weapp.appId,
          code: code,
          bUserId:bUserId
        },
        method: "POST",
        success: function (res) {
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          //console.log("userLogin complete:" + res)
        }
      })
    })
  },
  /**
   * 获得超级管理员openId
   */
  getSuperOpenId: function (code) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.getSuperOpenId,
        header: {
          "content-type": "application/json;charset=UTF-8",
        },
        data: {
          appId: config.weapp.appId,
          code: code,
        },
        method: "POST",
        success: function (res) {
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },
  /**
  * 获得手机号
  */
  getUserPhone: function (encryptedData, iv, superopenId, session_key) {
    return new Promise((resolve, reject) => {
      tt.request({
        url: host+config.api.getUserPhone,
        header: {
          "content-type": "application/json;charset=UTF-8",
        },
        data: {
          appId: config.weapp.appId,
          encryptedData: encryptedData,
          iv: iv,
          openId: superopenId,
          session_key: session_key,
        },
        method: "POST",
        success: function (res) {
          resolve(res)
        },
        fail: function (err) {
          reject(err)
        },
        complete: function (res) {
          //console.log("userLogin complete:" + res)
        }
      })
    })
  },
}
module.exports = func;