first commit
This commit is contained in:
1
衣莎项目/源码/YiShaXiYi/model/.pydio
Normal file
1
衣莎项目/源码/YiShaXiYi/model/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
183ec991-fd2b-4a5a-b4be-0fccf2a86310
|
||||
62
衣莎项目/源码/YiShaXiYi/model/getSetting.js
Normal file
62
衣莎项目/源码/YiShaXiYi/model/getSetting.js
Normal file
@@ -0,0 +1,62 @@
|
||||
const app = getApp();
|
||||
const config = app.loadConfig()
|
||||
|
||||
// 对StorageSync进行二次封装
|
||||
export class StorageSync {
|
||||
/**
|
||||
* 存储本地信息
|
||||
* @param {String} key 存储名称
|
||||
* @param {String} value 存储内容
|
||||
*/
|
||||
setStorage(key, value) {
|
||||
wx.setStorage({
|
||||
key: `${config.prefix}${key}`,
|
||||
data: value
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 获取本地存储数据
|
||||
* @param {String} key 需要获取的存储对象名称
|
||||
*/
|
||||
getSetting(key) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
wx.getStorage({
|
||||
key: `${config.prefix}${key}`,
|
||||
success(res) {
|
||||
resolve(res.data)
|
||||
},
|
||||
fail() {
|
||||
resolve()
|
||||
console.log("获取本地信息失败获取的key为:", key);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
deleteStorage(key){
|
||||
if(typeof key !== 'string') return console.error('类型错误');
|
||||
wx.removeStorageSync(`${config.prefix}${key}`)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取本地信息未完成
|
||||
export class GetSetting extends StorageSync {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
// 判断本地是否授权
|
||||
isAuthorization() {
|
||||
if (!!this.getSetting('token')) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 获取本地授权信息
|
||||
getAuthorization() {
|
||||
|
||||
}
|
||||
// 更新本地授权信息
|
||||
updateAuthorization() {
|
||||
|
||||
}
|
||||
}
|
||||
13
衣莎项目/源码/YiShaXiYi/model/index.js
Normal file
13
衣莎项目/源码/YiShaXiYi/model/index.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const { StorageSync } = require("./getSetting");
|
||||
const { Msg } = require("./message")
|
||||
const { Payment } = require("./payment")
|
||||
const { Getauth } = require("./user")
|
||||
const { ShopCart } = require("./shopCart")
|
||||
|
||||
module.exports = {
|
||||
storage: new StorageSync(),
|
||||
msg: new Msg(),
|
||||
payment: new Payment(),
|
||||
getauth: new Getauth(),
|
||||
shopCart: new ShopCart()
|
||||
}
|
||||
34
衣莎项目/源码/YiShaXiYi/model/message.js
Normal file
34
衣莎项目/源码/YiShaXiYi/model/message.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import { $Toast } from "./../extend/iview/dist/base/index"
|
||||
|
||||
export class Msg {
|
||||
/**
|
||||
* 提示消息
|
||||
* @param {string} content 提示内容
|
||||
* @param {string} type 内置的类型,可选值为 default,success,warning,error,loading String default
|
||||
* @param {Number} duration 持续时间,单位秒,设置为 0 则不自动关闭,需调用 $Toast.hide() 方法手动关闭
|
||||
* @param {String} icon 自定义图标
|
||||
* @param {String} image 自定义图片地址
|
||||
* @param {Boolean} mask 是否显示一个隐藏的遮罩层,点击遮罩层可立即关闭组件true
|
||||
* @param {String} selector 组件标识 #toast
|
||||
*/
|
||||
dataMsg(data) {
|
||||
data.mask = data.mask || false
|
||||
try {
|
||||
$Toast(data)
|
||||
} catch (error) {
|
||||
console.log(error, "提示消息出错")
|
||||
}
|
||||
}
|
||||
}
|
||||
// const dataMsg = (data) => {
|
||||
// data.mask = data.mask || false
|
||||
// try {
|
||||
// $Toast(data)
|
||||
// } catch (error) {
|
||||
// console.log(error, "提示消息出错")
|
||||
// }
|
||||
// }
|
||||
|
||||
// module.exports = {
|
||||
// dataMsg: dataMsg,
|
||||
// }
|
||||
103
衣莎项目/源码/YiShaXiYi/model/payment.js
Normal file
103
衣莎项目/源码/YiShaXiYi/model/payment.js
Normal file
@@ -0,0 +1,103 @@
|
||||
const app = getApp();
|
||||
const config = app.loadConfig()
|
||||
const api = app.loadApi()
|
||||
const regeneratorRuntime = app.loadAsync()
|
||||
const { Http } = require("../http/ajax")
|
||||
const http = new Http
|
||||
const model = app.loadModel("index")
|
||||
// 支付未完成等待看接口
|
||||
export class Payment {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
/**
|
||||
* 封装小程序支付模块,方便对支付的控制
|
||||
*/
|
||||
async pay(url, data) {
|
||||
let tel = await this.isTelnum()
|
||||
if(tel == 1) return Promise.resolve(3);
|
||||
return new Promise((resolve, reject) => {
|
||||
console.log(12);
|
||||
wx.showModal({
|
||||
content: '确定支付',
|
||||
cancelText: '取消',
|
||||
confirmText: '确认',
|
||||
async success(res) {
|
||||
if (res.confirm) {
|
||||
if (config.isPay) {
|
||||
let payInfo = await http.request2({ url: `${url}`, method: "POST", data: data })
|
||||
if (payInfo.data.data.type == 1 || payInfo.data.data.code == 200) {
|
||||
wx.showToast({
|
||||
title: '支付成功',
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
})
|
||||
resolve(1)
|
||||
}
|
||||
if (!!payInfo.data.data.nonceStr) {
|
||||
wx.requestPayment({
|
||||
timeStamp: payInfo.data.data.timeStamp,
|
||||
nonceStr: payInfo.data.data.nonceStr,
|
||||
package: payInfo.data.data.package,
|
||||
signType: 'md5',
|
||||
paySign: payInfo.data.data.paySign,
|
||||
success(res) {
|
||||
if (res.errMsg == "requestPayment:ok") {
|
||||
wx.showToast({
|
||||
title: '支付成功',
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
})
|
||||
resolve(1)
|
||||
}
|
||||
},
|
||||
fail(res) {
|
||||
wx.showToast({
|
||||
title: '取消支付',
|
||||
icon: 'none',
|
||||
duration: 1000
|
||||
})
|
||||
resolve(2)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: '支付功能已关闭',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
}
|
||||
} else if (res.cancel) {
|
||||
resolve(2)
|
||||
}
|
||||
},
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
isTelnum() {
|
||||
return new Promise((resolve, reject) => {
|
||||
let userInfo = app.globalData.userInfo
|
||||
if (typeof userInfo['telnum'] == 'string' && (userInfo['telnum'] == "未授权手机号" || userInfo['telnum'] == '')) {
|
||||
wx.showModal({
|
||||
title: '未授权手机号不能办卡哦!!!',
|
||||
content: '点击确定前往授权手机号',
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
resolve(1)
|
||||
wx.navigateTo({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
}else{
|
||||
resolve(1)
|
||||
}
|
||||
}
|
||||
})
|
||||
}else{
|
||||
resolve(2)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
133
衣莎项目/源码/YiShaXiYi/model/shopCart.js
Normal file
133
衣莎项目/源码/YiShaXiYi/model/shopCart.js
Normal file
@@ -0,0 +1,133 @@
|
||||
const app = getApp();
|
||||
const regeneratorRuntime = app.loadAsync()
|
||||
const { StorageSync } = require("./getSetting")
|
||||
const util = app.loadUtils("util")
|
||||
const { Msg } = require("../model/message")
|
||||
const msg = new Msg()
|
||||
|
||||
export class ShopCart extends StorageSync {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
/**
|
||||
* 获取本地购物车缓存如果没有数据返回空数组
|
||||
*/
|
||||
async getCart() {
|
||||
let that = this
|
||||
let shopCart = await that.getSetting("shopCart")
|
||||
if (typeof shopCart === 'undefined') {
|
||||
await this.setStorage("shopCart", [])
|
||||
return await []
|
||||
}
|
||||
return await shopCart
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param { Object } data 放到购物中的数据
|
||||
*/
|
||||
async setCart(data) {
|
||||
let getCart = await this.getCart()
|
||||
if (data.num <= 0) {
|
||||
data.num = ++data.num
|
||||
}
|
||||
if (getCart.length <= 0) {
|
||||
getCart.push(data)
|
||||
} else {
|
||||
for (let i = 0; i < getCart.length; i++) {
|
||||
if (util.Compare(getCart[i], data, "num")) {
|
||||
getCart[i].num += data.num
|
||||
break;
|
||||
}
|
||||
if (getCart.length - 1 == i) {
|
||||
getCart.push(data)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.setStorage("shopCart", getCart)
|
||||
// 更新角标
|
||||
app.globalData.cartNum = getCart.length
|
||||
wx.showToast({
|
||||
title: '添加成功,在购物车等亲~',
|
||||
icon: 'none',
|
||||
duration: 800
|
||||
})
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param { Object } data 已经更新过的数据
|
||||
*/
|
||||
async updateCart(data) {
|
||||
let that = this
|
||||
let getCart = await this.getCart()
|
||||
for (let i = 0; i < getCart.length; i++) {
|
||||
if (util.Compare(getCart[i], data, "num")) {
|
||||
if (data.num == 0) {
|
||||
getCart.splice(i, 1)
|
||||
break;
|
||||
}
|
||||
getCart[i].num = data.num
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
if (data.num == 0) {
|
||||
wx.showModal({
|
||||
title: '删除该商品',
|
||||
content: '点击确定删除',
|
||||
confirmText: "确认",
|
||||
cancelText: "取消",
|
||||
async success(res) {
|
||||
if (res.confirm) {
|
||||
that.setStorage("shopCart", getCart)
|
||||
// 更新角标
|
||||
app.globalData.cartNum = getCart.length
|
||||
resolve(getCart)
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this.setStorage("shopCart", getCart)
|
||||
// 更新角标
|
||||
app.globalData.cartNum = getCart.length
|
||||
resolve(getCart)
|
||||
}
|
||||
})
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
async getCartMon() {
|
||||
let cart = await this.getCart()
|
||||
let totalprice = 0
|
||||
cart.map((v, i) => {
|
||||
totalprice += v.num * v.price
|
||||
})
|
||||
return totalprice
|
||||
}
|
||||
/**
|
||||
* 购物车删除商品
|
||||
* @param { Object } data 需要删除的商品id
|
||||
*/
|
||||
async deleteCart(data) {
|
||||
let getCart = await this.getCart()
|
||||
let newCart = []
|
||||
let newData = data.allCart
|
||||
newData.map((v, i) => {
|
||||
if (typeof v.isShow == 'boolean') {
|
||||
delete newData[i].isShow
|
||||
}
|
||||
})
|
||||
getCart.map((v, i) => {
|
||||
let n = newData.filter(n => {
|
||||
return util.Compare(v, n, "num")
|
||||
})
|
||||
if (n.length <= 0) {
|
||||
newCart.push(v)
|
||||
}
|
||||
})
|
||||
this.setStorage("shopCart", newCart)
|
||||
// 更新角标
|
||||
app.globalData.cartNum = newCart.length
|
||||
}
|
||||
}
|
||||
126
衣莎项目/源码/YiShaXiYi/model/user.js
Normal file
126
衣莎项目/源码/YiShaXiYi/model/user.js
Normal file
@@ -0,0 +1,126 @@
|
||||
const app = getApp();
|
||||
const config = app.loadConfig()
|
||||
const regeneratorRuntime = app.loadAsync()
|
||||
const api = app.loadApi()
|
||||
const { StorageSync } = app.loadModel("getSetting")
|
||||
const storage = new StorageSync()
|
||||
|
||||
export class Getauth {
|
||||
/**
|
||||
* 获取用户当前授权过小程序的权限
|
||||
*/
|
||||
getSetting() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
wx.getSetting({
|
||||
success(res) {
|
||||
resolve(res)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 个人信息授权处理
|
||||
*/
|
||||
async userInfo() {
|
||||
// 未授权个人信息跳转到授权页面
|
||||
// let user = await this.getSetting()
|
||||
// if (!user.authSetting['scope.userInfo']) {
|
||||
// wx.redirectTo({
|
||||
// url: '/pages/login/login'
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
// await this.login()
|
||||
// 如果已授权跳转首页
|
||||
wx.redirectTo({
|
||||
url: '/pages/auth/auth?index=1'
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 地理位置授权 为了获取用户的地理位置
|
||||
*/
|
||||
async userLocation() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
wx.authorize({
|
||||
scope: 'scope.userLocation',
|
||||
success() {
|
||||
wx.getLocation({
|
||||
type: 'gcj02',
|
||||
success(res) {
|
||||
// 把地理信息存到本地
|
||||
storage.setStorage("position", res)
|
||||
wx.request({
|
||||
url: `${config.baseUrl}${api.lately}`, method: "POST", data: {
|
||||
lat: res.latitude,
|
||||
lng: res.longitude
|
||||
},
|
||||
success(rel) {
|
||||
resolve(rel.data.data)
|
||||
storage.setStorage("nearestStore", rel.data.data)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 相册授权
|
||||
*/
|
||||
albumAuthorization() {
|
||||
//获取相册授权
|
||||
return new Promise(function (resolve, reject) {
|
||||
wx.getSetting({
|
||||
success(res) {
|
||||
if (!res.authSetting['scope.writePhotosAlbum']) {
|
||||
wx.authorize({
|
||||
scope: 'scope.writePhotosAlbum',
|
||||
success() {
|
||||
resolve("ok")
|
||||
},
|
||||
fail() {
|
||||
resolve("err")
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
resolve("ok")
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 用户登录方法
|
||||
* 登陆成功把获取的token存到本地
|
||||
*/
|
||||
login(userInfo) {
|
||||
wx.showLoading({
|
||||
title: '登录中',
|
||||
})
|
||||
let data = userInfo || {};
|
||||
return new Promise(function (resolve, reject) {
|
||||
wx.login({
|
||||
success(res) {
|
||||
if (res.code) {
|
||||
wx.request({
|
||||
url: `${config.baseUrl}${api.login}`, method: "POST", data: { code: res.code, ...data },
|
||||
success(rel) {
|
||||
storage.setStorage("token", rel.data.data.token)
|
||||
resolve(rel)
|
||||
app.globalData.isToken = true
|
||||
wx.hideLoading()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
wx.hideLoading()
|
||||
console.log('登录失败!' + res.errMsg)
|
||||
}
|
||||
},
|
||||
fail() {
|
||||
wx.hideLoading()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user