203 lines
4.9 KiB
Markdown
203 lines
4.9 KiB
Markdown
# Stemcell_Commerce 模块说明
|
|
|
|
## 概述
|
|
|
|
Stemcell_Commerce 模块为Magento 2系统提供微信小程序集成功能,实现了微信小程序用户的创建、登录和信息管理等核心功能。
|
|
|
|
## 功能特性
|
|
|
|
1. **微信小程序用户创建**:通过微信小程序的 `getPhoneNumber` 接口获取用户手机号,并自动在Magento系统中创建用户
|
|
2. **用户信息修改**:支持修改用户的姓名、邮箱、手机号和公司单位信息
|
|
3. **微信一键登录**:用户通过微信openid一键登录,并返回访问令牌(access token)
|
|
4. **添加用户公司属性**:为Magento用户实体添加"公司/单位"自定义属性
|
|
|
|
## 安装配置
|
|
|
|
### 1. 启用模块
|
|
|
|
```bash
|
|
bin/magento module:enable Stemcell_Commerce
|
|
bin/magento setup:upgrade
|
|
bin/magento setup:di:compile
|
|
bin/magento cache:clean
|
|
```
|
|
|
|
### 2. 配置微信小程序参数
|
|
|
|
在Magento后台进入:**Stores > Configuration > Stemcell > 微信小程序配置**,设置以下参数:
|
|
|
|
- **AppID**:微信小程序的AppID
|
|
- **AppSecret**:微信小程序的AppSecret
|
|
- **微信API地址**:默认值为 `https://api.weixin.qq.com`
|
|
|
|
## API接口说明
|
|
|
|
### 1. 创建微信小程序用户
|
|
|
|
**URL**: `/V1/stemcell-commerce/customer/create-by-wechat`
|
|
**Method**: `POST`
|
|
**权限**: 匿名访问
|
|
|
|
**参数**:
|
|
```json
|
|
{
|
|
"encryptedData": "微信小程序加密的手机号数据",
|
|
"iv": "加密算法的初始向量",
|
|
"code": "微信登录临时凭证"
|
|
}
|
|
```
|
|
|
|
**返回值**:
|
|
```json
|
|
{
|
|
"id": 123,
|
|
"firstname": "微信用户",
|
|
"lastname": "",
|
|
"email": "wx_13800138000_abcdef@stemcell.com",
|
|
"custom_attributes": [
|
|
{
|
|
"attribute_code": "mobile",
|
|
"value": "13800138000"
|
|
}
|
|
],
|
|
// 其他用户信息...
|
|
}
|
|
```
|
|
|
|
### 2. 更新用户信息
|
|
|
|
**URL**: `/V1/stemcell-commerce/customer/update-info`
|
|
**Method**: `POST`
|
|
**权限**: 需要用户认证
|
|
|
|
**参数**:
|
|
```json
|
|
{
|
|
"customerId": 123,
|
|
"name": "张三",
|
|
"email": "zhangsan@example.com",
|
|
"mobile": "13800138000",
|
|
"company": "示例公司"
|
|
}
|
|
```
|
|
|
|
**返回值**:
|
|
```json
|
|
{
|
|
"id": 123,
|
|
"firstname": "张三",
|
|
"lastname": "",
|
|
"email": "zhangsan@example.com",
|
|
"custom_attributes": [
|
|
{
|
|
"attribute_code": "mobile",
|
|
"value": "13800138000"
|
|
},
|
|
{
|
|
"attribute_code": "company",
|
|
"value": "示例公司"
|
|
}
|
|
],
|
|
// 其他用户信息...
|
|
}
|
|
```
|
|
|
|
### 3. 微信一键登录
|
|
|
|
**URL**: `/V1/stemcell-commerce/customer/wechat-login`
|
|
**Method**: `POST`
|
|
**权限**: 匿名访问
|
|
|
|
**参数**:
|
|
```json
|
|
{
|
|
"code": "微信登录临时凭证"
|
|
}
|
|
```
|
|
|
|
**返回值** (已绑定手机的用户):
|
|
```json
|
|
{
|
|
"access_token": "用户访问令牌",
|
|
"token_type": "Bearer",
|
|
"expires_in": 86400,
|
|
"customer": {
|
|
"id": 123,
|
|
"firstname": "张三",
|
|
"lastname": "",
|
|
"email": "zhangsan@example.com",
|
|
// 其他用户信息...
|
|
}
|
|
}
|
|
```
|
|
|
|
**返回值** (新用户,需要绑定手机):
|
|
```json
|
|
{
|
|
"need_bind_phone": true,
|
|
"openid": "微信用户的openid"
|
|
}
|
|
```
|
|
|
|
## 使用示例
|
|
|
|
### 微信小程序端调用示例
|
|
|
|
```javascript
|
|
// 微信小程序登录
|
|
wx.login({
|
|
success: res => {
|
|
if (res.code) {
|
|
// 调用微信一键登录API
|
|
wx.request({
|
|
url: 'https://your-magento-site.com/rest/V1/stemcell-commerce/customer/wechat-login',
|
|
method: 'POST',
|
|
data: { code: res.code },
|
|
success: response => {
|
|
if (response.data.need_bind_phone) {
|
|
// 需要绑定手机号
|
|
// 调用getPhoneNumber接口获取用户手机号
|
|
wx.getUserProfile({
|
|
desc: '用于完善用户资料',
|
|
success: userProfile => {
|
|
// 调用获取手机号接口
|
|
wx.request({
|
|
url: 'https://your-magento-site.com/rest/V1/stemcell-commerce/customer/create-by-wechat',
|
|
method: 'POST',
|
|
data: {
|
|
encryptedData: encryptedData,
|
|
iv: iv,
|
|
code: res.code
|
|
},
|
|
success: createRes => {
|
|
console.log('用户创建成功', createRes.data);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
} else {
|
|
// 登录成功,保存token
|
|
wx.setStorageSync('token', response.data.access_token);
|
|
console.log('登录成功', response.data);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
```
|
|
|
|
## 授权访问其他API
|
|
|
|
登录成功后,所有需要用户认证的API都需要在请求头中添加以下授权信息:
|
|
|
|
```
|
|
Authorization: Bearer {access_token}
|
|
```
|
|
|
|
## 注意事项
|
|
|
|
1. 确保已在微信小程序管理后台正确配置了域名白名单
|
|
2. 模块会自动为用户创建临时邮箱,格式为:`wx_{手机号}_{随机字符串}@stemcell.com`
|
|
3. 如需自定义临时邮箱后缀,可以修改 `CustomerManagement` 类中的 `generateTempEmail` 方法
|
|
4. 数据补丁会自动为用户实体添加"company"属性 |